package com.xsmart.oa.services;

import com.baomidou.mybatisplus.plugins.Page;
import com.xsmart.common.constant.ResultConstant;
import com.xsmart.common.entity.RequestBean;
import com.xsmart.common.entity.ResponseBean;
import com.xsmart.oa.dao.SystemParamConfigDAO;
import org.apache.ibatis.annotations.Update;
import org.apache.tomcat.util.bcel.classfile.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;
/**
 * @author lxh
 * @date 2017/11/29
 */
@Service
public class SystemConfigService {
    @Autowired
    SystemParamConfigDAO systemParamConfigDAO;

    /**
     * 插入系统参数
     * @param requestBean
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseBean insertSystemParam(RequestBean requestBean){
        ResponseBean responseBean=requestBean.getResponseBean();
        String paramCode=requestBean.getRequestParamString("paramCode");
        String paramName=requestBean.getRequestParamString("paramName");
        String paramGroupCode=requestBean.getRequestParamString("paramGroupCode");
        String paramGroupText=requestBean.getRequestParamString("paramGroupText");
        String paramValue=requestBean.getRequestParamString("paramValue");
        Integer paramStatus=requestBean.getRequestParamInteger("paramStatus");
        //插入系统参数
        try{
            int flag=systemParamConfigDAO.insertSystemParam(paramCode,paramName,paramGroupCode,paramGroupText,paramValue,paramStatus);
            responseBean.setResult(ResultConstant.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            responseBean.setResult(ResultConstant.ERROR);
            throw new RuntimeException();

        }
        return responseBean;
    }

    /**
     * 查询系统参数
     * @param requestBean
     * @return
     */
    public ResponseBean querySystemParam(RequestBean requestBean){
        ResponseBean responseBean =requestBean.getResponseBean();
        int currentPage=requestBean.getRequestParamInteger("currentPage",1);
        int pageSize=requestBean.getRequestParamInteger("pageSize",10);
        String paramCode=requestBean.getRequestParamString("paramCode");
        String paramName=requestBean.getRequestParamString("paramName");
        Page<Map<String,Object>> page=new Page<>(currentPage,pageSize);
        //查询系统参数
        page.setRecords(systemParamConfigDAO.queryCfgsSysParam(page,paramCode,paramName));
        responseBean.addResultPageItem("systemParam",page);
        responseBean.setResult(ResultConstant.SUCCESS);
        return responseBean;
    }

    /**
     * 更新系统参数
     * @param requestBean
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseBean updateSystemParam(RequestBean requestBean){
        ResponseBean responseBean=requestBean.getResponseBean();
        String paramCode=requestBean.getRequestParamString("paramCode");
        String paramName=requestBean.getRequestParamString("paramName");
        String paramGroupCode=requestBean.getRequestParamString("paramGroupCode");
        String paramGroupText=requestBean.getRequestParamString("paramGroupText");
        String paramValue=requestBean.getRequestParamString("paramValue");
        Integer paramStatus=requestBean.getRequestParamInteger("paramStatus");
        Integer paramId=requestBean.getRequestParamInteger("paramId");
        //更新系统参数
        try{
            int flag=systemParamConfigDAO.updateCfgsSysParam(paramCode,paramName,paramGroupCode,paramGroupText,paramValue,paramStatus,paramId);
            responseBean.setResult(ResultConstant.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            responseBean.setResult(ResultConstant.ERROR);
            throw new RuntimeException();
        }

        return responseBean;
    }
}
